﻿<%@ Page Async="true" AutoEventWireup="true" CodeBehind="Login.aspx.cs" Inherits="AssetManage.Account.Login" Language="C#" MasterPageFile="~/Site.Master" Title="登陆" %>

<%@ MasterType VirtualPath="~/Site.Master" %>

<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
<h2><%: Title %></h2>
<script type="text/javascript" src="/Scripts/CryptoJS/sha256.js"></script>
<script type="text/javascript" src="/Scripts/CryptoJS/enc-base64-min.js"></script>
<script type="text/javascript">
    var UserID = "#<%= UserID.ClientID %>";
    var UserID_Error = "#<%= UserID_Error.ClientID %>";

    var Password = "#<%= Password.ClientID %>";
    var Password_Error = "#<%= Password_Error.ClientID %>";

    var Captcha = "#<%= Captcha.ClientID %>";
    var Captcha_Error = "#<%= Captcha_Error.ClientID %>";

    var UserLogin = "#UserLogin";

    var ReturnURL = "<%= Request.QueryString["ReturnUrl"] %>";

    $(function() {
        $(UserLogin).attr("disabled", "disabled");

        $(UserID).change(function() {
            $(UserLogin).attr("disabled", "disabled");
            if ($(UserID).val() === "") {
                $(UserID_Error).text("请输入用户名。");
            } else {
                var reg = /^[a-zA-Z]\w{3,15}$/;
                if (!reg.test($(UserID).val())) {
                    $(UserID_Error).text("请输入正确的用户名。");
                } else {
                    if (("#" + this.id) === UserID) {
                        var sendData = {
                            section: "userid",
                            userid: $(UserID).val()
                        };
                        $.ajax({
                            type: "POST",
                            url: "/Handler/Validate",
                            data: sendData,
                            beforeSubmit: function() {
                                $(UserID_Error).text("正在验证……");
                            },
                            success: function(response) {
                                var responseData = eval("(" + response + ")");
                                if (responseData.error === "Cookie_Error") {
                                    window.location.replace("/Error/Cookie");
                                    return;
                                }
                                if (!responseData.exist) {
                                    $(UserID_Error).text("该帐户不存在。输入其他账户或注册新的帐户。");
                                } else {
                                    $(UserID_Error).text("");
                                    if (HaveNoError()) {
                                        $(UserLogin).removeAttr("disabled");
                                    }
                                }
                            }
                        });
                    }
                }
            }
        });

        $(Password).change(function() {
            $(UserLogin).attr("disabled", "disabled");
            if ($(Password).val() === "") {
                $(Password_Error).text("请输入密码。");
            } else {
                var reg = /^([a-zA-Z0-9-`=\\\[\];',./~!@#$%\^&*()_+|{}:"<>?]){6,16}$/;
                if (!reg.test($(Password).val())) {
                    $(Password_Error).text("请输入正确的密码。");
                } else {
                    $(Password_Error).text("");
                    if (HaveNoError()) {
                        $(UserLogin).removeAttr("disabled");
                    }
                }
            }
        });

        $(Captcha).change(function() {
            $(UserLogin).attr("disabled", "disabled");
            if ($(Captcha).val() === "") {
                $(Captcha_Error).text("请输入验证码。");
            } else {
                var reg = /^[^jiloxJILOX10]{5}$/;
                if (!reg.test($(Captcha).val())) {
                    $(Captcha_Error).text("请输入正确的验证码。");
                } else {
                    if (("#" + this.id) === Captcha) {
                        var sendData = {
                            section: "captcha",
                            captcha: $(Captcha).val()
                        };
                        $.ajax({
                            type: "POST",
                            url: "/Handler/Validate",
                            data: sendData,
                            beforeSubmit: function() {
                                $(Captcha_Error).text("正在验证……");
                            },
                            success: function(response) {
                                var responseData = eval("(" + response + ")");
                                if (responseData.success) {
                                    $(Captcha_Error).text("");
                                    if (HaveNoError()) {
                                        $(UserLogin).removeAttr("disabled");
                                    }
                                } else {
                                    $(Captcha_Error).text("请输入正确的验证码。");
                                    if (responseData.error === "Cookie_Error") {
                                        window.location.replace("/Error/Cookie");
                                        return;
                                    }
                                }
                            }
                        });
                    }
                }
            }
        });

        $("#CaptchaImg").click(function() {
            $("#CaptchaImg").attr("src", "/Handler/Captcha?action=get&random=" + Math.random());
            $(Captcha).val("");
            $(Captcha_Error).text("请输入验证码。");
        });

        $(UserLogin).click(function() {
            if (!HaveNoError()) {
                $(UserLogin).attr("value", "登陆");
                $(UserLogin).attr("disabled", "disabled");
                return;
            }

            $(UserLogin).attr("value", "正在验证身份……");
            $(UserLogin).attr("disabled", "disabled");

            var sendData =
            {
                action: "login",
                captcha: $(Captcha).val(),
                userid: $(UserID).val(),
                password: CryptoJS.SHA256($(Password).val()).toString(CryptoJS.enc.Hex).toUpperCase()
            };
            $.ajax({
                type: "POST",
                url: "/Handler/Account",
                data: sendData,
                success: function(response) {
                    var responseData = eval("(" + response + ")");
                    if (responseData.success) {
                        $(UserID_Error).text("");
                        $(Password_Error).text("");
                        if (ReturnURL === "") {
                            ReturnURL = "/";
                        }
                        window.location.href = ReturnURL;
                    } else {
                        $(UserLogin).attr("value", "登陆");
                        $(UserLogin).attr("disabled", "disabled");
                        $("#CaptchaImg").attr("src", "/Handler/Captcha?action=get&random=" + Math.random());
                        $(Captcha).val("");
                        switch (responseData.error) {
                        case "Captcha_Error":
                            $(Captcha_Error).text("请输入正确的验证码。");
                            break;
                        case "Password_Error":
                            $(Password_Error).text("该密码不正确。请确保使用与你的帐户对应的密码。");
                            break;
                        case "UserID_Error":
                            $(UserID_Error).text("该帐户不存在。输入其他账户或注册新的帐户。");
                            $(Password_Error).text("");
                            break;
                        case "Cookie_Error":
                            window.location.replace("/Error/Cookie");
                            return;
                        }
                    }
                }
            });
        });
    });

    function HaveNoError() {
        var result = true;
        $(".form-horizontal :text").each(function() {
            if ($(this).val() === "") {
                result = false;
            }
        });
        $(".form-horizontal :password").each(function() {
            if ($(this).val() === "") {
                result = false;
            }
        });
        $("[id$='_Error']").each(function() {
            if ($(this).text() !== "") {
                result = false;
            }
        });
        return result;
    }
</script>
<div class="form-horizontal">
    <h4>使用本地帐户登录。</h4>
    <hr/>
    <div class="form-group">
        <asp:Label runat="server" AssociatedControlID="UserID" CssClass="col-md-2 control-label">用户名</asp:Label>
        <div class="col-md-10">
            <asp:TextBox runat="server" ID="UserID" CssClass="form-control" MaxLength="16"/>
            <asp:Label ID="UserID_Error" runat="server" CssClass="text-danger"></asp:Label>
        </div>
    </div>
    <div class="form-group">
        <asp:Label runat="server" AssociatedControlID="Password" CssClass="col-md-2 control-label">密码</asp:Label>
        <div class="col-md-10">
            <asp:TextBox runat="server" data-toggle="tooltip" data-placement="bottom" title="密码长度至少6位，至多16位。" ID="Password" TextMode="Password" CssClass="form-control" MaxLength="16"/>
            <asp:Label ID="Password_Error" runat="server" CssClass="text-danger"></asp:Label>
        </div>
    </div>
    <div class="form-group">
        <asp:Label runat="server" AssociatedControlID="Captcha" CssClass="col-md-2 control-label">验证码</asp:Label>
        <div class="col-md-10">
            <img id="CaptchaImg" alt="点击更换图片" data-toggle="tooltip" data-placement="bottom" title="点击更换图片" src="/Handler/Captcha" class="form-control" style="border: none; cursor: pointer; height: 60px; width: 280px;"/>
            <asp:TextBox data-toggle="tooltip" data-placement="bottom" title="验证码不区分大小写" runat="server" ID="Captcha" MaxLength="5" CssClass="form-control"/>
            <asp:Label ID="Captcha_Error" runat="server" CssClass="text-danger"></asp:Label>
        </div>
    </div>
    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input id="UserLogin" type="button" value="登陆" class="btn btn-primary btn-lg"/>
            <asp:Label runat="server">&nbsp;&nbsp;&nbsp;</asp:Label>
            <asp:HyperLink runat="server" ID="RegisterHyperLink" ViewStateMode="Disabled">注册新用户</asp:HyperLink>
            <asp:Label runat="server">&nbsp;&nbsp;&nbsp;</asp:Label>
            <asp:HyperLink runat="server" ID="ResetChangepasswordHyperLink" ViewStateMode="Disabled" NavigateUrl="~/Account/ResetPassword">重置密码</asp:HyperLink>
        </div>
    </div>
</div>
</asp:Content>